package com.base.service.impl;

import java.util.List;

import com.base.service.DictService;
import com.common.bean.Datadict;
import com.common.bean.Dict;
import com.common.util.AbstractManager;
import com.common.util.PagerModel;

public class DictServiceImpl extends AbstractManager implements DictService {

	public void addDictDetail(Datadict obj,String typecode) {
		Dict dict = (Dict) this.getHibernateTemplate().get(Dict.class, typecode);
		obj.setDict(dict);
		obj.setStatus("1");
		this.getHibernateTemplate().save(obj);
	}

	public List findAllDict() {
		String hql = "from Dict";
		return this.getHibernateTemplate().find(hql);  
	}

	public List findDictDetail(String typecode) {
		String hql = "from Datadict dt where dt.dict.typecode = ?" ;
		return this.getHibernateTemplate().find(hql,typecode);
	}
	
	public PagerModel findAllDict(int offset , int pagesize) {
		String hql = "from Dict";
		return this.searchPaginated(hql, offset, pagesize);
	}
	
	public PagerModel findDictDetail(String typecode , int offset , int pagesize) {
		String hql = "from Datadict dt where dt.dict.typecode = ?" ;
		return this.searchPaginated(hql, typecode, offset, pagesize);
	}

	public void removeDict(String typecode) {
		Dict obj = (Dict)this.getHibernateTemplate().get(Dict.class, typecode);
		if(obj.getDatadicts().size() > 0) {
			throw new RuntimeException("含有子项，不能删除！");
		}
		this.getHibernateTemplate().delete(obj);
	}

	public void removeDictDetail(Long id) {
		Datadict obj = (Datadict)this.getHibernateTemplate().get(Datadict.class, id);
		this.getHibernateTemplate().delete(obj);
	}

	public void addDict(Dict obj) {
		this.getHibernateTemplate().save(obj);
	}
}
